import sys
import pokeparser
import re

dex = open('text/dex.txt', 'r', encoding='utf-8')
move = open('text/move.txt', 'r', encoding='utf-8')
lst = open('text/listlearn.txt', 'a+', encoding='utf-8')
p = pokeparser.pokeparser()

pids = {}
gends = []
for pk in dex:
	pkm = p.convert(pk)
	pids[pkm[1]] = pkm[0]
	if pkm[8] == '-1':
		gends.append(pkm[0])

# 不能使用招式学习器的宝可梦。测试了一下，所有宝可梦都能学的招式学习器，以下3个列表的宝可梦都不能学：
gen8ban = ['013','014','132','235','265','266','268','664','789','824']
gen7ban = ['010','011','129','374','415','790']
gen5ban = ['201','202','360','401','412','602']
# 所有宝可梦都能学的招式。键=例外宝可梦id，值=招式id串
allcanlearn = {}
allcan = '001～([0-9]{3}):对应世代中所有可以使用招式学习器的宝可梦'

# 字典learn=每只宝可梦可以学的招式。键=宝可梦id，值=招式id串
learn = {}
# 将宝可梦和招式关联。pkm=宝可梦id，move=招式id。
def learnmove(pkm,move):
	# 将中文转换为符号，配合form.txt：
	if re.search('[0-9]{3} 阿罗拉的样子',pkm):
		pkm = pkm.replace(' 阿罗拉的样子','a')
	elif re.search('[0-9]{3} 伽勒尔的样子',pkm):
		pkm = pkm.replace(' 伽勒尔的样子','g')
	elif re.search('[0-9]{3} 雄性',pkm):
		pkm = pkm.replace(' 雄性','♂')
	elif re.search('[0-9]{3} 雌性',pkm):
		pkm = pkm.replace(' 雌性','♀')
	elif re.search('[0-9]{3} 超极巨化',pkm):
		pkm = pkm.replace(' 超极巨化','-gigantamax')
	elif re.search('[0-9]{3} 搭档宝可梦',pkm):
		pkm = pkm.replace(' 搭档宝可梦','-partner')
	elif re.search('[0-9]{3} 普通形态',pkm):
		pkm = '386=normal'
	elif re.search('[0-9]{3} 攻击形态',pkm):
		pkm = '386-attack'
	elif re.search('[0-9]{3} 防御形态',pkm):
		pkm = '386-defence'
	elif re.search('[0-9]{3} 速度形态',pkm):
		pkm = '386-speed'
	elif re.search('[0-9]{3} 草木蓑衣',pkm):
		pkm = '413=plant'
	elif re.search('[0-9]{3} 砂土蓑衣',pkm):
		pkm = '413=sandy'
	elif re.search('[0-9]{3} 垃圾蓑衣',pkm):
		pkm = '413=trash'
	elif re.search('[0-9]{3} 切割',pkm):
		pkm = '479-mow'
	elif re.search('[0-9]{3} 加热',pkm):
		pkm = '479-heat'
	elif re.search('[0-9]{3} 旋转',pkm):
		pkm = '479-fan'
	elif re.search('[0-9]{3} 清洗',pkm):
		pkm = '479-wash'
	elif re.search('[0-9]{3} 结冰',pkm):
		pkm = '479-frost'
	elif re.search('[0-9]{3} 别种形态',pkm):
		pkm = '487=altered'
	elif re.search('[0-9]{3} 起源形态',pkm):
		pkm = '487-origin'
	elif re.search('[0-9]{3} 陆上形态',pkm):
		pkm = '492=land'
	elif re.search('[0-9]{3} 天空形态',pkm):
		pkm = '492-sky'
	elif re.search('[0-9]{3} 暗黑酋雷姆',pkm):
		pkm = '646-black'
	elif re.search('[0-9]{3} 焰白酋雷姆',pkm):
		pkm = '646-white'
	elif re.search('[0-9]{3} 惩戒胡帕',pkm):
		pkm = '720=confined'
	elif re.search('[0-9]{3} 解放胡帕',pkm):
		pkm = '720-unbound'
	elif re.search('[0-9]{3} 白昼的样子',pkm):
		pkm = '745=midday'
	elif re.search('[0-9]{3} 黄昏的样子',pkm):
		pkm = '745=dusk'
	elif re.search('[0-9]{3} 黑夜的样子',pkm):
		pkm = '745=midnight'
	elif re.search('[0-9]{3} 黄昏之鬃',pkm):
		pkm = '800-dusk'
	elif re.search('[0-9]{3} 拂晓之翼',pkm):
		pkm = '800-dawn'
	elif re.search('[0-9]{3} 高调',pkm):
		pkm = '849=amped'
	elif re.search('[0-9]{3} 低调',pkm):
		pkm = '849=lowkey'
	elif re.search('[0-9]{3} 剑之王',pkm):
		pkm = '888-crowned'
	elif re.search('[0-9]{3} 盾之王',pkm):
		pkm = '889-crowned'
	elif re.search('[0-9]{3} 一击流',pkm):
		pkm = '892=single'
	elif re.search('[0-9]{3} 连击流',pkm):
		pkm = '892=rapid'
	elif re.search('[0-9]{3} 骑白马',pkm):
		pkm = '898-ice'
	elif re.search('[0-9]{3} 骑黑马',pkm):
		pkm = '898-shadow'
	
	if (pkm in learn) == False:
		learn[pkm] = []
	if (move in learn[pkm]) == False:
		learn[pkm].append(move)

# 列表ml。元素=招式信息。
ml = []
for line in move:
	ml.append(line)

lens = len(ml)
count = 0

# 将move.txt中的招式：宝可梦映射 转换为宝可梦：招式映射
while count < lens:
	m = p.convert(ml[count])
	mid = m[0]
	pkms = m[12].split('/')
	for pkm in pkms:
		match = re.search(allcan,pkm)
		if match:
			ban = []
			mx = match.group(1)
			rg = int(mx)
			al = '001～'+mx+':对应世代中所有可以使用招式学习器的宝可梦'
			pkm = pkm.replace(al,'')
			ban = (gen5ban + gen7ban + gen8ban).copy()
			if re.search('除外',pkm):
				pkm = pkm.replace('除外','')
				pkml = pkm.strip(' ').split('、')
				for excp in pkml:
					if excp in pids:
						ban.append(pids[excp])
					elif excp == '无性别的宝可梦':
						ban = (ban + gends).copy()
			ban = sorted(set(ban))
			moverange = mid+':'+mx
			allcanlearn[moverange] = ban
		else:
			learnmove(pkm, mid)
	count += 1

# 让宝可梦其他形态学习一定范围内宝可梦都能使用的招式：
for moverange in allcanlearn:
	mr = moverange.split(':')
	mid = mr[0]
	rg = int(mr[1])
	ban = allcanlearn[moverange]
	count = 0
	while count < rg:
		count += 1
		pid = '{num:03}'.format(num=count)
		if (pid in ban) == False:
			learnmove(pid,mid)
			if rg > 721 and (pid+'a') in learn:
				learnmove(pid+'a',mid)
			if rg > 809 and (pid+'g') in learn:
				learnmove(pid+'g',mid)

learn = sorted(learn.items(),key=lambda x: x[0])
temp = sys.stdout
sys.stdout = lst

for pkm in learn:
	info = []
	info.append(pkm[0])
	info.append('/'.join(sorted(set(pkm[1]))))
	print(info)

lst.flush()

sys.stdout = temp
